package cn.itlaobing.web.servlet;

import cn.itlaobing.entity.VegetablesDTO;
import cn.itlaobing.exception.BusinessException;
import cn.itlaobing.service.VegetableService;
import cn.itlaobing.util.PageUtil;
import cn.itlaobing.web.annotation.RequestMapping;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.List;

/**
 * @Description:
 * @Author: WangDong
 * @Date: 2017/11/10 19:31
 */
@WebServlet("/vegetableservlet/*")
public class VegetableServlet extends HttpServlet {


    @RequestMapping("/record")
  public void query(HttpServletRequest request,HttpServletResponse response) throws IOException, ServletException {
        request.setCharacterEncoding("utf-8");
        PageUtil pageUtil=new PageUtil();
        VegetableService vegetableService=new VegetableService();
        pageUtil.setRecordCount(10);
        String strPageIndex=request.getParameter("pageIndex");
        if(strPageIndex!=null && !strPageIndex.equals("")){
            pageUtil.setPageIndex(Integer.parseInt(strPageIndex));
        }
        try {
           // List<VegetablesDTO> list=vegetableService.findAll(pageUtil.getPageIndex(),pageUtil.getPageSize());
            List<VegetablesDTO> list=vegetableService.queryAll();
            System.out.println(list.size());
            request.setAttribute("pageUtil",pageUtil);
            request.setAttribute("DATAS",list);
            response.sendRedirect(request.getContextPath()+"/record.jsp");
        } catch (BusinessException e) {
            e.printStackTrace();
        }
    }
    @RequestMapping("/update")
  public void update(HttpServletRequest request,HttpServletResponse response) throws IOException, ServletException {
      request.setCharacterEncoding("utf-8");
      String id=request.getParameter("id");
      String vegetablename=request.getParameter("vegetablename");
      String quantity=request.getParameter("quantity");
      VegetablesDTO vegetablesDTO=new VegetablesDTO();
      vegetablesDTO.setId(id);
      vegetablesDTO.setName(vegetablename);
      vegetablesDTO.setQuantity(Float.parseFloat(quantity));
      VegetableService vegetableService=new VegetableService();
      try {
        int r=vegetableService.update(vegetablesDTO);

      } catch (BusinessException e) {
        request.getRequestDispatcher("/WEB-INF/update.jsp").forward(request,response);
      }
      response.sendRedirect(request.getContextPath()+"/record.jsp");
    }
  @RequestMapping("/delete")
  public void delete(HttpServletRequest request,HttpServletResponse response) throws IOException {
    request.setCharacterEncoding("utf-8");
    String id=request.getParameter("id2");
    VegetableService vegetableService=new VegetableService();
    try {
      int r=vegetableService.delete(id);
      if(r>0){
        response.sendRedirect(request.getContextPath()+"/record.jsp");
        return;
      }else{
        response.sendRedirect(request.getContextPath()+"/record.jsp");
        return;
      }
    } catch (BusinessException e) {
      response.sendRedirect(request.getContextPath()+"/record.jsp");
      return;
    }

  }
}
